The present invention concerns handling of uninitialized local variables by a compiler which utilizes optimization. Particularly, the present invention provides for the detection and correction of uninitialized local variables in a subroutine.
Programs are generally written in a high level programming language. This high level language, often referred to as source code, is translated by a compiler program into an assembly language. The binary form of the assembly language, called object code, is the form of the code actually executed by a computer.
When writing source code for compilation, it is generally good programming protocol to make sure each local variable is correctly initialized before it is used. Many compilers, for example, those which do not optimize code or those which do not optimize code by assigning local variables to registers, will assign variables to stack memory locations initially containing zero (or some other acceptable value). This may allow compiled code to execute as the programmer intended, despite the failure to initialize certain local variables before they are used.
However, some optimizing compilers may assign variables to a register rather than to a stack memory location. This is done because object code executes faster when resources are stored in registers, rather than memory locations. Registers, however, due to their heavy usage, are much less likely to be initialized to zero or some other acceptable value. The result can be code which, when executed, can produce results which surprise a programmer who failed to initialize all local variables before their use.
At least one prior art source code processing tool has provided for the issuance of a warning when a local variable is found for which there cannot possibly be a reaching definition. See, for example, the tool "Lint" which is intended to detect flaws in source code and is distributed as part of the UNIX operating system (UNIX is a trademark of AT&T Bell Laboratories). However, this tool has no provision for adding a default definition when none is available. Moreover, no warning is output if a local variable is defined only on some of the reaching control flow paths.